package com.wholesale;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wholesale.beans.GoodsInfo;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.GoodsListAdapter;

public class SearchResultLayout extends LinearLayout implements OnClickListener {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private ListView bodyList;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;
	private String keyWords;
	private Button newButton;
	private Button lowButton;
	private Button highButton;
	private RelativeLayout loadingLayout;
	private String shortBy = "lastUpdate";

	private GoodsListAdapter listAdapter;
	private GoodsListAsync goodsListAsync;
	private Drawable newon, newoff, lowtohighon, lowtohighoff, hightolowon,
			hightolowoff;

	// 构造函数
	public SearchResultLayout(Context context, final ViewFlipper viewFlipper,
			String keyWords) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		this.keyWords = keyWords;
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.goodslist, this);
		bodyList = (ListView) baseView.findViewById(R.id.bodyList);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText("Back");
		leftButton.setOnClickListener(this);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setVisibility(View.INVISIBLE);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText("Search Result");
		loadingLayout = (RelativeLayout) baseView
				.findViewById(R.id.loadingLayout);

		bodyList.setSelector(R.drawable.details_list_bg);
		// item 单击事件
		bodyList.setOnItemClickListener(moreClieckListener);
		listAdapter = new GoodsListAdapter(context, bodyList);
		listAdapter.setAsyncLoadData(true);
		bodyList.setAdapter(listAdapter);
		newButton = (Button) baseView.findViewById(R.id.newBtn);
		newButton.setOnClickListener(this);
		lowButton = (Button) baseView.findViewById(R.id.lowBtn);
		lowButton.setOnClickListener(this);
		highButton = (Button) baseView.findViewById(R.id.highBtn);
		highButton.setOnClickListener(this);
		// 加载数据
		goodsListAsync = new GoodsListAsync(context);
		goodsListAsync.execute(true);

		newon = this.getResources()
				.getDrawable(R.drawable.details_segment_1_on);
		newoff = this.getResources().getDrawable(
				R.drawable.details_segment_1_off);
		lowtohighon = this.getResources().getDrawable(
				R.drawable.details_segment_2_on);
		lowtohighoff = this.getResources().getDrawable(
				R.drawable.details_segment_2_off);
		hightolowon = this.getResources().getDrawable(
				R.drawable.details_segment_3_on);
		hightolowoff = this.getResources().getDrawable(
				R.drawable.details_segment_3_off);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		case R.id.rightBtn:
			break;
		case R.id.newBtn:
			shortBy = "lastUpdate";
			newButton.setBackgroundDrawable(newon);
			lowButton.setBackgroundDrawable(lowtohighoff);
			highButton.setBackgroundDrawable(hightolowoff);
			if (goodsListAsync != null) {
				goodsListAsync.cancel(true);
			}
			goodsListAsync = new GoodsListAsync(context);
			goodsListAsync.execute(true);
			break;
		case R.id.lowBtn:
			shortBy = "shopPrice";
			newButton.setBackgroundDrawable(newoff);
			lowButton.setBackgroundDrawable(lowtohighon);
			highButton.setBackgroundDrawable(hightolowoff);
			if (goodsListAsync != null) {
				goodsListAsync.cancel(true);
			}
			goodsListAsync = new GoodsListAsync(context);
			goodsListAsync.execute(false);
			break;
		case R.id.highBtn:
			shortBy = "shopPrice";
			newButton.setBackgroundDrawable(newoff);
			lowButton.setBackgroundDrawable(lowtohighoff);
			highButton.setBackgroundDrawable(hightolowon);
			if (goodsListAsync != null) {
				goodsListAsync.cancel(true);
			}
			goodsListAsync = new GoodsListAsync(context);
			goodsListAsync.execute(true);
			break;
		default:
			break;
		}

	}

	// ListView 点击事件
	OnItemClickListener moreClieckListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			GoodsDetailLayout goodsDetailLayout = new GoodsDetailLayout(
					context, viewFlipper, "Back", listAdapter.getItem(arg2));
			viewFlipper.addView(goodsDetailLayout);
			viewFlipper.showNext();
		}
	};

	class GoodsListAsync extends
			AsyncTask<Boolean, Integer, ArrayList<GoodsInfo>> {
		ProgressDialog pdialog;
		long goodsCount = 1;
		boolean desc;

		public GoodsListAsync(Context context) {
			// pdialog = new ProgressDialog(context);
			// pdialog.setTitle("Loading...");
			// pdialog.setMessage("Please wait...");
			// pdialog.setCancelable(true);
			// pdialog.setButton("cancel", new DialogInterface.OnClickListener()
			// {
			// public void onClick(DialogInterface dialog, int i) {
			// dialog.cancel();
			// GoodsListAsync.this.cancel(true);
			// }
			// });
			// pdialog.show();
		}

		@Override
		protected void onPreExecute() {
			listAdapter.stop();
			listAdapter.clear();
			listAdapter.notifyDataSetChanged();
			loadingLayout.setVisibility(View.VISIBLE);
		}

		@Override
		protected ArrayList<GoodsInfo> doInBackground(Boolean... params) {
			desc = params[0];
			goodsCount = WebServiceClient
					.getGoodsListByQueryPageCount(keyWords);
			if (goodsCount == 0) {
				return new ArrayList<GoodsInfo>();
			} else {
				return WebServiceClient.getGoodsListByQuery(keyWords, 1,
						shortBy, desc);
			}
		}

		@Override
		protected void onPostExecute(ArrayList<GoodsInfo> result) {
			listAdapter.clear();
			listAdapter.addItems(result);
			listAdapter.notifyDataSetChanged();
			listAdapter.setPageNum(GoodsListAdapter.ACTION_SEARCHGOODSLIST,
					keyWords, (int) goodsCount, "shopPrice", desc);
			// pdialog.dismiss();
			loadingLayout.setVisibility(View.GONE);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}
}
